<template>
  <div class="productmangent">
    <div class="headerBox">
      <el-button type="primary" @click="addproduct">新增产品</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%; margin-bottom: 20px">
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="name" label="产品名称"> </el-table-column>
      <el-table-column prop="typeName" label="产品类型"> </el-table-column>
      <el-table-column prop="createName" label="添加人"> </el-table-column>
      <el-table-column prop="updateTime" label="添加时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="editProduct(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteProduct(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-size="formData.pageSize"
      :current-page="formData.pageNum"
      @current-change="SetPage"
      @size-change="handleSizeChange"
    ></el-pagination>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="25%">
      <el-form :model="addformdata" :rules="rules" ref="appFrom" label-width="80px">
        <el-form-item label="产品类型">
          <el-select v-model="addformdata.type" placeholder="请选择产品类型" v-el-select-loadmore="loadmore">
            <el-option v-for="(item, index) in optionList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input v-model="addformdata.name" autocomplete="off" maxlength="15" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveProduct">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getAppList, getTypeList, addProduct, deleteProduct } from "@/api/basisMG";
export default {
  directives: {
    "el-select-loadmore": {
      bind(el, binding) {
        const SELECTWRAP_DOM = el.querySelector(
          ".el-select-dropdown .el-select-dropdown__wrap"
        );
        SELECTWRAP_DOM.addEventListener("scroll", function () {
          const condition = this.scrollHeight - this.scrollTop <= this.clientHeight;
          if (condition) {
            binding.value();
          }
        });
      },
    },
  },
  data() {
    return {
      title: "",
      dialogFormVisible: false,
      tableData: [],
      formData: {
        type: "",
        name: "",
        pageSize: 10,
        pageNum: 1,
      },
      total: 0,
      optionList: [],
      formTypeData: {
        type: 0,
        productType: "",
        pageNum: 1,
        pageSize: 10,
      },
      rules: {
        name: [{ required: true, message: "请填写类型名称", trigger: "blur" }],
      },
      addformdata: {
        name: "",
        note: "",
        type: "",
        icon: "",
        id: "",
      },
      pages: "",
      typePage: 0,
    };
  },
  methods: {
    // 分页
    SetPage(e) {
      this.formData.pageNum = e;
      this.getAppList();
    },
    handleSizeChange (val) {
      this.formData.pageSize = val;
      this.getAppList();
    },
    // 懒加载
    loadmore() {
      if (this.formTypeData.pageNum++ < this.typePage) {
        this.getTypeList();
      }
    },
    async getAppList() {
      const res = await getAppList(this.formData);
      this.tableData = res.data.content;
      this.total = res.data.total;
      this.pages = res.data.pages;
    },
    addproduct() {
      this.title = "新增产品";
      this.dialogFormVisible = true;
      this.formTypeData.pageNum = 1;
      this.getTypeList();
      this.addformdata.name = "";
      this.addformdata.typeName = "";
      this.addformdata.type = "";
      this.addformdata.id = "";
    },
    async getTypeList() {
      const res = await getTypeList(this.formTypeData);
      if (res.data.total > 10) {
        this.optionList = this.optionList.concat(res.data.content);
        this.typePage = res.data.pages;
      } else {
        this.optionList = res.data.content;
        this.typePage = 1;
      }
    },
    saveProduct() {
      this.$refs["appFrom"].validate(async (valid) => {
        if (valid) {
          const data = await addProduct(this.addformdata);
          if (data.code != 200) return this.$message.error(data.message);
          this.$message.success(data.message);
          setTimeout(() => {
            this.dialogFormVisible = false;
            this.getAppList();
          }, 500);
        } else {
          this.$message.warning("提交失败, 请检查内容是否填写正确!!!");
          return false;
        }
      });
    },
    editProduct(row) {
      this.title = "编辑产品";
      this.dialogFormVisible = true;
      this.addformdata.name = row.name;
      this.addformdata.typeName = row.typeName;
      this.addformdata.type = row.type;
      this.addformdata.id = row.id;
      async () => {
        const data = await addProduct(this.addformdata);
        if (data.code != 200) return this.$message.error(data.message);
        this.$message.success(data.message);
        setTimeout(() => {
          this.dialogFormVisible = false;
          this.getAppList();
        }, 500);
      };
    },
    deleteProduct(id) {
      this.$confirm("确定要删除吗？删除后将无法恢复，请谨慎操作", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const data = await deleteProduct({id:id});
          if (data.code != 200) return this.$message.error("操作失败请稍后重试");
          this.getAppList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getAppList();
  }
};
</script>
<style lang="scss">
.productmangent {
  padding: 20px;
  .headerBox {
    margin-bottom: 20px;
  }
}
</style>
